/**
 * @author ccpalu
 */

function closeUploadBox(){
	if (document.getElementById('otherOptionDiv').style.display==''){
		closeOtherOptionMenu();
	}
	else{ 
		document.getElementById('addSamples').style.display='';
		document.getElementById('addSamples').src='http://cnviewer.googlecode.com/hg/Images/addSamples.png';
	}
	document.getElementById('sourceDiv').style.display='none';
	
	return false;
}

function filesResourceBox(){
	try{
		if (cnv.uploadKind=="complementary"){
			otherOptionMenuDisplay();
		}
		else {
			if (document.getElementById('projectListD')) {
				if ((document.getElementById('projectListD').style.display)||(document.getElementById('sourceDiv').style.display=='none')) {
					document.getElementById('addSamples').src='http://cnviewer.googlecode.com/hg/Images/addSamples_.png';
					document.getElementById('fileUploadForm').style.display = 'none';
					document.getElementById('fileLoginForm').style.display = 'none';
					document.getElementById('filePasteForm').style.display = 'none';
					document.getElementById('otherOptionDiv').style.display="none";
					resourceFirstBoxDisplay();
					document.getElementById('sourceDiv').style.display = '';
				}
				else {
					document.getElementById('fileLoginForm').style.display = '';
				}
				document.getElementById('projectListD').style.display = 'none';
			}
			else {
				document.getElementById('addSamples_').src='http://cnviewer.googlecode.com/hg/Images/addSamples.png';
				document.getElementById('fileUploadForm').style.display = 'none';
				document.getElementById('fileLoginForm').style.display = 'none';
				document.getElementById('filePasteForm').style.display = 'none';
				document.getElementById('otherOptionDiv').style.display="none";
				resourceFirstBoxDisplay();
				document.getElementById('sourceDiv').style.display = '';
			}
		}
	}
	catch(e){
		if (document.getElementById('projectListD')) {
			if ((document.getElementById('projectListD').style.display)||(document.getElementById('sourceDiv').style.display=='none')) {
				document.getElementById('addSamples').src='http://cnviewer.googlecode.com/hg/Images/addSamples_.png';
				document.getElementById('fileUploadForm').style.display = 'none';
				document.getElementById('fileLoginForm').style.display = 'none';
				document.getElementById('filePasteForm').style.display = 'none';
				document.getElementById('otherOptionDiv').style.display="none";
				resourceFirstBoxDisplay();
				document.getElementById('sourceDiv').style.display = '';
			}
			else {
				document.getElementById('fileLoginForm').style.display = '';
			}
			document.getElementById('projectListD').style.display = 'none';
			document.getElementById('addSamples').src='http://cnviewer.googlecode.com/hg/Images/addSamples_.png';//added in 20/09/2010
		}
		else {
			document.getElementById('addSamples').src='http://cnviewer.googlecode.com/hg/Images/addSamples_.png';
			document.getElementById('fileUploadForm').style.display = 'none';
			document.getElementById('fileLoginForm').style.display = 'none';
			document.getElementById('filePasteForm').style.display = 'none';
			document.getElementById('otherOptionDiv').style.display="none";
			document.getElementById('sourceDiv').style.display = '';
			resourceFirstBoxDisplay();
		}
	}
	return false;
}

function resourceFirstBoxDisplay(){
	//if ((document.getElementById('dataSourceP').style.display=='none')||(document.getElementById('sourceDiv').style.display=='none')){
	if (document.getElementById('dataSourceP').style.display=='none'){
		var disp = '';
		document.getElementById('backUploadForm').style.display='none';
	}
	else{
		var disp = 'none';
		document.getElementById('backUploadForm').style.display='';
	}
	if (document.getElementById('otherOptionDiv').style.display==''){
		closeOtherOptionMenu();
	}
	document.getElementById('dataSourceP').style.display=disp;
	document.getElementById('s3dbFileB').style.display=disp;
	document.getElementById('addFileB').style.display=disp;
	document.getElementById('pasteFileB').style.display=disp;
	document.getElementById('hUpload').style.display=disp;

	return disp;
}

function filesResourceBClick(){
	if (document.getElementById('addSamples').src.match("addSamples_.png")){
		closeUploadBox();
	}
	else{
		if (document.getElementById('otherOptionDiv').style.display==''){
			closeOtherOptionMenu();
		}
		document.getElementById('sourceDiv').style.display="";
		if (document.getElementById('dataSourceP').style.display=='none'){
			filesResourceBox();
		}
	}
	return false;
}
function uploadDiv(){
	buttonImageA('addSamples', 'http://cnviewer.googlecode.com/hg/Images/addSamples.png', filesResourceBClick, 0, 50, 12, 210, 0, 0, 'Display resource menu').style.display='none';
	var w = 300, h= 250;
	var sourceDiv = document.createElement('div');
	sourceDiv.id='sourceDiv';
	sourceDiv.style.width=w+'px';
	sourceDiv.style.height=h+'px';
	sourceDiv.style.zIndex="400";
	setPosition(sourceDiv, 275,215);
	document.body.appendChild(sourceDiv);

	if (document.doctype){
		var surface = dojox.gfx.createSurface(sourceDiv, w, h);
		
		var rect1 = {
			x: 0,
			y: 0,
			width: w,
			height: h,
			r: 10
		};
		var lg1 = {
			type: "linear",
			x1: 0,
			y1: 0,
			x2: 0,
			y2: 200,
			x3: 70,
			y3: 250,
			x4: 300,
			y4: 400,
			x5: 550,
			y5: 300,
			
			colors: [{
				offset: 0,
				color: "rgba(255,255,255,0)"
			}, 
			{
				offset: 0.1,
				color: "#B0DC85"//"#b3ce7c"
			},
			{
				offset: 0.6,
				color: "#b1cc7a"
			},
			{
				offset: 0.9,
				color: "#B3DEA0"//"#9BEC75"//"#dde879"
			},
					{
				offset: 1,
				color: "rgba(255,255,255,0)"
			}]
		};
			
			
		var vDetail = surface.createRect(rect1).setFill(lg1).setTransform({
			dx: 3,
			dy: 0
		});
		vDetail.getEventSource().setAttribute('id','vRectDetail');
	}
	else{ //April 2011
		var configBg= document.createElement("img");
		configBg.src = "Images/bgMenu.png";
		configBg.id = "bgMenu";
		sourceDiv.appendChild(configBg);
		configBg.width=w;
		configBg.height=h-30;
	}
	var goBack=buttonImageA('backUploadForm', 'http://cnviewer.googlecode.com/hg/Images/goBack2.png', goBackBClick, 20, 0, 255, 20, 0, sourceDiv, 'Return');
	goBack.style.display='none';
	goBack.style.zIndex=100;
	
	var close=buttonImageA('closeUploadForm', 'http://cnviewer.googlecode.com/hg/Images/close2.png', closeUploadBox, 20, 0, 275, 18, 0, sourceDiv, 'Close');
	close.style.zIndex=100;
	
	addHelp ('hUpload', 'help', 'http://sites.google.com/site/cnviewerguide/home/input-files/loading-data', 'sourceDiv', 19, 264, 12);
	
	return sourceDiv;
}

function loadOptions(){
	var parent = uploadDiv();
	var divTitle = createParagraph('dataSourceP', 'Choose a Data Source', parent);
	setPosition(divTitle, 57, 30);
	setFont(divTitle,'18px',0,0,0);
	
	buttonImageA('s3dbFileB', 'http://cnviewer.googlecode.com/hg/Images/s3dbLogo.png', loginS3DBform, 100, 0, 20, 75, 0, parent, 'Files from S3DB');

	if (location.host == "cnviewer.googlecode.com")	{
		buttonImageA('addFileB', 'http://cnviewer.googlecode.com/hg/Images/addFileDisab.png', disabledReceivingFile, 120, 0, 160, 75, 0, parent, 'Text file');
	}
	else {
		buttonImageA('addFileB', 'http://cnviewer.googlecode.com/hg/Images/addFile.png', uploadFile, 120, 0, 160, 75, 0, parent, 'Text file');
	}
	buttonImageA('pasteFileB', 'http://cnviewer.googlecode.com/hg/Images/CutCopyPasteTitle.png', pasteFile, 150, 0, 85, 120, 0, parent, 'Paste data');
	cnvObject();
	createUploadForm();
	createLoginForm();
	createPasteForm();
	otherOptionMenu();
	return false;
}

function goBackBClick(){
	if ((document.getElementById('addSamples').src.match("addSamples_.png"))||(document.getElementById('addSamples').style.display=="none")){
		filesResourceBox();
	}
	else{
		otherOptionMenuDisplay();
	}
	return false;
}

function uploadFile() {
	var otherOption=document.getElementById("otherOptionDiv");
	if (otherOption.style.display=='none'){
		resourceFirstBoxDisplay();
	}
	else{
		document.getElementById("otherOptionDiv").style.display='none';
		document.getElementById('backUploadForm').style.display='';
	}
	document.getElementById('fileUploadForm').style.display='';
	return false;
}

function disabledReceivingFile(){
		alert('This option is not available at this URL.\n'+
			'Please use Copy/Paste or access CNViewer through\n'+
			'http://cnviewer.googlecode.com/hg/index.html');
		return false;
}


function loginS3DBform() {
	resourceFirstBoxDisplay();
	if (document.getElementById('selectProject')){
		document.getElementById('projectListD').style.display='';
	}
	else{
	document.getElementById('fileLoginForm').style.display='';
	}
	return false;
}

function loginS3DB(){
	loadingStatus('Logging in S3DB');
	var urlKey = document.getElementById('domainId').value+"/apilogin.php?username="+document.getElementById('userId').value +
			"&password=" + document.getElementById('userPassword').value+"&authotity=s3db";
	s3db_jsonpp_call(urlKey, "getKey(ans)");
	//closeUploadBox();

return false;
}

function pasteFile() {
	var otherOption=document.getElementById("otherOptionDiv");
	if (otherOption.style.display=='none'){
		resourceFirstBoxDisplay();
	}
	else{
		document.getElementById("otherOptionDiv").style.display='none';
		document.getElementById('backUploadForm').style.display='';
	}
	document.getElementById('filePasteForm').style.display='';
	return false;
}


function buttonImageA(id, file, func, w, h, l, t, position, parentObj, alt){
	
	var bI=document.createElement('IMG');

	if (parentObj)
		parentObj.appendChild(bI);
	else
		document.body.appendChild(bI);

	bI.id=id;
	bI.src=file;
	bI.onclick=func;
	bI.style.cursor='pointer';
	bI.alt = alt;
	bI.title = alt;

		if (position) 
			bI.style.position = position;
		else 
			bI.style.position = 'absolute';
		if (w) 
			resize(bI, w);
		if (h) 
			bI.style.height = h + 'px';
		
		if (l) 
			bI.style.left = l + 'px';
		if (t) 
			bI.style.top = t + 'px';
	
		return bI;
}

function createNewFiles(){
	var newFiles = document.createElement('OBJECT');
	newFiles.id='newFiles';
	newFiles.uploads=new Array();
	newFiles.segData = new Array();
	newFiles.index=0;
	newFiles.dataMap = new Array();
	document.body.appendChild(newFiles);
	
	newFiles.xAxis='';
	newFiles.ch = new Array();
	newFiles.chRegions = new Array(23);
	newFiles.extractedSegData = new Array(23);
	for (var i=1; i<=22; i=i+1){
		newFiles.chRegions[i] = new Array();
	}
	newFiles.sampleIndex=0;
	newFiles.userData=new Array();
	newFiles.userData['seg'] = new Array();
	newFiles.userData['name'] = new Array();
	newFiles.userData['map'] = new Array();
	newFiles.userData['index'] = new Array();
	newFiles.mapAnswer=new Array();
	newFiles.max=new Array(22);
	newFiles.min=new Array(22);

	for (var i=0; i<22; i=i+1){
		newFiles.max[i]=0;
		newFiles.min[i]=90000000000;
	}
	newFiles.realSize=chSize();
	newFiles.ready=false;
	newFiles.similarity = new Array();

	newFiles.legends = new Array();
	return false;
}

function createLoginForm(){
	var loginForm = document.createElement('FORM');
	loginForm.id='fileLoginForm';
	setPosition(loginForm, 15,1);
	document.getElementById('sourceDiv').appendChild(loginForm);
	var l=20, l2=l+80;
	
	addHelp ('hS3DB', 'help', 'http://sites.google.com/site/cnviewerguide/home/input-files/loading-data/loading-data-from-s3db', 'fileLoginForm', 19, 224, 12);
	
	var s3dbLogo = document.createElement('IMG');
	loginForm.appendChild(s3dbLogo);
	s3dbLogo.id='s3dbLogo';
	s3dbLogo.src='http://cnviewer.googlecode.com/hg/Images/s3db.png';
	resize(s3dbLogo,40);
	setPosition(s3dbLogo, 35, 25, 'absolute');
		
	var divTitle = createParagraph('loginFileP', 'Login at S3DB', loginForm);
	divTitle.style.width="200px";
	setFont(divTitle,'18px',0,0,0);
	setPosition(divTitle, 80, 30,'absolute');
		
	var userLabel=createParagraph('userLabel', 'User ID:', loginForm);
	userLabel.style.width="200px";;
	setFont(userLabel,'16px',0,0,0);
	setPosition(userLabel, l, 70, 'absolute');
	
	var loginUser = document.createElement('INPUT');
	loginUser.id='userId';
	loginUser.type='text';
	loginUser.name='userId';
	loginUser.value='public';
	loginUser.style.width='120px';
	setFont(loginUser,'16px',0,0,0);
	loginForm.appendChild(loginUser);
	setPosition(loginUser, l2, 69, 'absolute');

	var passwordLabel=createParagraph('passwordLabel', 'Password:', loginForm);
	passwordLabel.style.width="150px";
	setFont(passwordLabel,'16px',0,0,0);
	loginForm.appendChild(passwordLabel);
	setPosition(passwordLabel, l, 95, 'absolute');

	var loginPassword = document.createElement('INPUT');
	loginPassword.type='password';
	loginPassword.id='userPassword';
	loginPassword.style.width='120px';
	loginPassword.name='userPassword';
	loginPassword.value='public';
	loginForm.appendChild(loginPassword);
	if (window.navigator.appName=="Netscape")
		setPosition(loginPassword, l2, 97,'absolute');
	else
		setPosition(loginPassword, l2, 96,'absolute');
	
	var authorityLabel=createParagraph('authorityLabel', 'Authority:', loginForm);
	authorityLabel.style.width="150px";
	setFont(authorityLabel,'16px',0,0,0);
	setPosition(authorityLabel, l, 120, 'absolute');
	loginForm.appendChild(authorityLabel);
	
	var selectAuthority=document.createElement('SELECT');
	selectAuthority.id=('selectAuthority');
	selectAuthority.name=('selectAuthority');
	if (window.navigator.appName=="Netscape")
		selectAuthority.style.width="124px";
	else
		selectAuthority.style.width="125px";
	loginForm.appendChild(selectAuthority);

	var authorityOptions=["S3DB", "s3db", "MDAnderson", "mdanderson", "Google", "google"];
	for (var i = 0; i < authorityOptions.length/2; i++) {
		selectAuthority.options[i] = new Option(authorityOptions[i*2], authorityOptions[i*2+1]);
	}
	if (window.navigator.appName=="Netscape")
		setPosition(selectAuthority, l2, 122,'absolute');
	else	
		setPosition(selectAuthority, l2, 120,'absolute');
	
	var domainLabel=createParagraph('domainLabel', 'Domain:', loginForm);
	domainLabel.style.width="150px";
	setFont(domainLabel,'16px',0,0,0);
	setPosition(domainLabel, l, 145, 'absolute');
	loginForm.appendChild(domainLabel);
	
	var loginDomain = document.createElement('INPUT');
	loginDomain.id='domainId';
	loginDomain.type='text';
	loginDomain.name='domainId';
	loginDomain.value='http://ibl.mdanderson.org/CNViewer';
	loginDomain.style.width='120px';
	setFont(loginDomain,'16px',0,0,0);
	loginForm.appendChild(loginDomain);
	setPosition(loginDomain, l2, 145, 'absolute');
	
	var loginButton = document.createElement('INPUT');
	loginButton.type='button';
	loginButton.id='loginButton';
	loginButton.name='loginButton';
	loginButton.value='Go';
	loginButton.onclick=loginS3DB;
	loginForm.appendChild(loginButton);
	setPosition(loginButton, l2+130, 143,'absolute');
	
	loginForm.style.display='none';
	
	return false;
}
	
function createUploadForm(){
	var uploadForm = document.createElement('FORM');
	uploadForm.id='fileUploadForm';
	uploadForm.action='cnvb.php';//'http://ibl.mdanderson.org/~ccpalu/CNVBalfa/cnvb.php';//'cnvb.php';
	uploadForm.enctype='multipart/form-data';
	uploadForm.method='post';
	uploadForm.target='uploadTarget';
	setPosition(uploadForm, 15,1);
	document.getElementById('sourceDiv').appendChild(uploadForm);
	uploadForm.style.display='none';

	addHelp ('hFileUpload', 'help', 'http://sites.google.com/site/cnviewerguide/home/input-files/loading-data/uploading-text-files', 'fileUploadForm', 19, 224, 12);
	
	var folderLogo = document.createElement('IMG');
	folderLogo.id='folderLogo';
	folderLogo.src='http://cnviewer.googlecode.com/hg/Images/folder.png';
	resize(folderLogo,50);
	setPosition(folderLogo, 35, 25, 'absolute');
	uploadForm.appendChild(folderLogo);
	
	var divTitle = createParagraph('uploadFileP', 'Choose a File', uploadForm);
	divTitle.style.width="200px";
	setFont(divTitle,'18px',0,0,0);
	setPosition(divTitle, 90, 30,'absolute');
	
	var uploadFile = document.createElement('INPUT');
	uploadFile.id='file';
	uploadFile.type='file';
	uploadFile.size='27';
	uploadFile.name='file';
	uploadFile.onchange=checkFile;
	uploadForm.appendChild(uploadFile);
	setPosition(uploadFile, 5, 90, 'absolute');
	
	var uploadIframe = document.createElement('IFRAME');
	uploadIframe.id='uploadTarget';
	uploadIframe.src='';
	uploadIframe.name='uploadTarget';
	uploadIframe.style.display='none';
	uploadForm.appendChild(uploadIframe);

	var fileLimit = createParagraph('fileLimitP', 'Upload maximum file size 2MB', uploadForm);
	fileLimit.style.width="200px";
	setFont(fileLimit,'13px',0,0,0);
	setPosition(fileLimit, 6, 142,'absolute');
	
	var submitFile = document.createElement('INPUT');
	submitFile.type='submit';
	submitFile.id='submitFile';
	submitFile.value='Upload';
	submitFile.name='action';
	submitFile.onclick=receivingFile;
	submitFile.disabled=true;
	uploadForm.appendChild(submitFile);
	setPosition(submitFile, 200, 135,'absolute');
	
	return false;
}

function createPasteForm(){
	var pasteForm = document.createElement('FORM');
	pasteForm.id='filePasteForm';
	setPosition(pasteForm, 15,1);
	document.getElementById('sourceDiv').appendChild(pasteForm);
	
	addHelp ('hS3DB', 'help', 'http://sites.google.com/site/cnviewerguide/home/input-files/loading-data/copy-paste-data', 'filePasteForm', 19, 224, 12);
	
	var folderLogo = document.createElement('IMG');
	folderLogo.id='pasteLogo';
	folderLogo.src='http://cnviewer.googlecode.com/hg/Images/CutCopyPaste.png';
	resize(folderLogo,70);
	setPosition(folderLogo, 10, 25, 'absolute');
	pasteForm.appendChild(folderLogo);
	
	var divTitle = createParagraph('pasteFileP', 'Paste your Data here', pasteForm);
	divTitle.style.width="200px";
	setFont(divTitle,'18px',0,0,0);
	setPosition(divTitle, 70, 30,'absolute');
	
	var pasteFile = document.createElement('INPUT');
	pasteFile.id='pasteText';
	pasteFile.type='text';
	pasteFile.style.width="260px";
	pasteFile.style.height="50px";
	pasteFile.maxLength=5000000;
	pasteForm.appendChild(pasteFile);
	setPosition(pasteFile, 5, 80, 'absolute');

	
	if (navigator.userAgent.match(/Opera/i))	{
		var charLimit = createParagraph('charLimitP', 'Opera supports up to 4 million characters.', pasteForm);
		charLimit.style.width="200px";
		setFont(charLimit,'13px',0,0,0);
		setPosition(charLimit, 7, 147,'absolute');
	}
	else{
		if ((navigator.userAgent.match(/Safari/i)) || (navigator.userAgent.match(/Chrome/i)))	{
			var charLimit = createParagraph('charLimitP', 'Safari and Chrome support around 540 thousand characters.', pasteForm);
			charLimit.style.width="200px";
			setFont(charLimit,'13px',0,0,0);
			setPosition(charLimit, 7, 147,'absolute');
		}
	}
	
	var submitText = document.createElement('INPUT');
	submitText.type='button';
	submitText.id='submitText';
	submitText.value='Send';
	submitText.onclick=readingText;
	pasteForm.appendChild(submitText);
	setPosition(submitText, 210, 145,'absolute');
	
	pasteForm.style.display='none';
	
	return false;
}

function receivingFile(){
	loadingStatus("Reading file ...");
	//return false;
}

function readingText(){
	if (document.getElementById('pasteText').value!="") {
		loadingStatus("Reading file ...");
		
		document.getElementById('CNV').uploads[0] = document.getElementById('pasteText').value;
		dataStorage();
	}
	//return false;
}
function checkFile(){
	if (document.getElementById('file').value)
		document.getElementById('submitFile').disabled=false;
	else
		document.getElementById('submitFile').disabled=true;
		
	return false;
}